package org.huamoxi.util;

import org.bukkit.Material;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.ArrayList;
import java.util.List;

public class InventoryUtil {

    /**
     * 校验容器是否是市场容器
     * @param inventory
     * @param config
     * @return
     */
    public static boolean checkMarketInventory (Inventory inventory, FileConfiguration config) {
        ItemStack lastPage = inventory.getItem(45);
        ItemStack marketSign = inventory.getItem(49);
        ItemStack nextPage = inventory.getItem(53);
        if (lastPage != null && marketSign != null && nextPage != null) {
            ItemMeta lastPageMeta = lastPage.getItemMeta();
            ItemMeta marketSignMeta = marketSign.getItemMeta();
            ItemMeta nextPageMeta = nextPage.getItemMeta();
            if (lastPageMeta.getDisplayName().equals(config.getString("notice.lastPageMsg"))
                && marketSignMeta.getDisplayName().equals(config.getString("notice.signTitle"))
                && nextPageMeta.getDisplayName().equals(config.getString("notice.nextPageMsg"))
            ) {
                return true;
            }
        }
        return false;
    }

    public static void setMarketPaper (Inventory inventory, FileConfiguration config, int current, int maxPage) {
        // 设置上一页
        ItemStack lastPaper = new ItemStack(Material.PAPER);
        ItemMeta lastPaperMeta = lastPaper.getItemMeta();
        if (lastPaperMeta != null) {
            lastPaperMeta.setDisplayName(config.getString("notice.lastPageMsg"));

            List<String> loreList = new ArrayList<>();
            if (current == 1) {
                loreList.add(config.getString("notice.isStartPageMsg"));
            } else {
                loreList.add(config.getString("notice.showLastPageMsg"));
            }
            lastPaperMeta.setLore(loreList);
            lastPaper.setItemMeta(lastPaperMeta);
        }
        inventory.setItem(45, lastPaper);

        // 设置下一页
        ItemStack nextPaper = new ItemStack(Material.PAPER);
        ItemMeta nextPaperMeta = nextPaper.getItemMeta();
        if (nextPaperMeta != null) {
            nextPaperMeta.setDisplayName(config.getString("notice.nextPageMsg"));

            List<String> loreList = new ArrayList<>();
            if (current == maxPage) {
                loreList.add(config.getString("notice.isEndPageMsg"));
            } else {
                loreList.add(config.getString("notice.showNextPageMsg"));
            }
            nextPaperMeta.setLore(loreList);
            nextPaper.setItemMeta(nextPaperMeta);
        }
        inventory.setItem(53, nextPaper);
    }

    public static void setMarketSign (Inventory inventory, FileConfiguration config) {
        // 设置木牌
        ItemStack sign = new ItemStack(Material.SPRUCE_SIGN);
        ItemMeta signItemMeta = sign.getItemMeta();
        signItemMeta.setDisplayName(config.getString("notice.signTitle"));
        List<String> signLoreList = new ArrayList<>();
        signLoreList.add("§2当前查看的是 --> §b市场的商品列表");
        if (!"".equals(config.getString("notice.signContent1"))) {
            signLoreList.add(config.getString("notice.signContent1"));
        }
        if (!"".equals(config.getString("notice.signContent1"))) {
            signLoreList.add(config.getString("notice.signContent2"));
        }
        if (!"".equals(config.getString("notice.signContent1"))) {
            signLoreList.add(config.getString("notice.signContent3"));
        }
        signItemMeta.setLore(signLoreList);
        sign.setItemMeta(signItemMeta);
        inventory.setItem(49, sign);
    }

    public static void setGlassPane (Inventory inventory, FileConfiguration config) {
        // 分类切换
        // 创建一个品红色染色玻璃块的ItemStack
        ItemStack stainedGlassPaneLeft = new ItemStack(Material.PINK_STAINED_GLASS_PANE);
        ItemMeta leftGlassMeta = stainedGlassPaneLeft.getItemMeta();
        leftGlassMeta.setDisplayName(config.getString("market.leftGlassTitle"));
        List<String> leftGlassMetaLore = new ArrayList<>();
        leftGlassMetaLore.add(config.getString("market.leftGlass"));
        leftGlassMeta.setLore(leftGlassMetaLore);
        stainedGlassPaneLeft.setItemMeta(leftGlassMeta);
        inventory.setItem(48, stainedGlassPaneLeft);

        ItemStack stainedGlassPaneRight = new ItemStack(Material.PINK_STAINED_GLASS_PANE);
        ItemMeta rightGlassMeta = stainedGlassPaneRight.getItemMeta();
        rightGlassMeta.setDisplayName(config.getString("market.rightGlassTitle"));
        List<String> rightGlassMetaLore = new ArrayList<>();
        rightGlassMetaLore.add(config.getString("market.rightGlass"));
        rightGlassMeta.setLore(rightGlassMetaLore);
        stainedGlassPaneRight.setItemMeta(rightGlassMeta);
        inventory.setItem(50, stainedGlassPaneRight);
    }
}
